import OSS from 'ali-oss'
import md5 from 'js-md5'

const client = new OSS({
  region: 'oss-cn-hangzhou',
  accessKeyId: 'LTAI4G7gKbqrZDkDEw1LXHA5',
  accessKeySecret: 'p1eX4hH7GymRZLfnhkUFMQlU2P7rAt',
  bucket: 'vchat-res'
})

const upload = (file, type) => {
  const dateNow = Date.now()
  const year = String((new Date()).getFullYear())
  const month = String((new Date()).getMonth() + 1)
  const day = String((new Date()).getDate())
  const filename = 'conosole/' + type + '/' + year + '/' + (month < 10 ? '0' + month : month) + '/' + (day < 10 ? '0' + day : day) + '/' + md5(dateNow + 'web') + '.' + file.type.split('/')[1]
  return new Promise(resolve => {
    client.put(filename, file).then(resp => {
      if (resp.res.status === 200) {
        // resolve(resp.url.replace('http://ddz-static.oss-cn-hangzhou.aliyuncs.com', 'https://static.chinajy1688.com'))
        resolve(resp.url)
      } else {
        this.showToast('上传失败')
      }
    })
  })
}
export default upload
